﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SQLServerHealthCheck.Model;
using AG.Utilities;

namespace SQLServerHealthCheck.SQLServer.IssueTypes
{
    public class DatabaseCheckNoNonClusteredIndexes : IssueTypeCheckBase
    {
        public DatabaseCheckNoNonClusteredIndexes()
        {
            IssueTypeId = 3016;
        }

        public override void Check()
        {
            try
            {
                // Create a Data Context 
                HealthCheckDataContext dc = new HealthCheckDataContext(System.Configuration.ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"]);
                Issue issue;

                ThresholdValue minSize = dc.ThresholdValues.FirstOrDefault(t => t.IssueTypeID == IssueTypeId && t.ThresholdValueID == 1);
                ThresholdValue indexesTablesRatio = dc.ThresholdValues.FirstOrDefault(t => t.IssueTypeID == IssueTypeId && t.ThresholdValueID == 2);
                // Create a link Query ( query Selects a database that has the MDF file stored on the C Drive 
                var qry = from d in dc.Databases
                          where d.SourceDatabaseId == null
                          orderby d.DatabaseName
                          select new
                          {    DatabaseID = d.DatabaseID,
                              name = d.DatabaseName,
                              tables = d.Tables.Count(),
                              indexes = (
                                            from t in d.Tables
                                            from i in t.Indexes
                                            where i.IndexType == "NONCLUSTERED INDEX"
                                            select i.IndexName).Count(),
                              size = (from s in d.DatabaseFiles
                                      select s.SizeInMb).Sum()


                          };
                var nclust = from v in qry
                             where ((v.tables * (indexesTablesRatio.Value)) > v.indexes *10 ) && (v.size > minSize.Value )
                            select v;
                

                foreach (var database in nclust)
                {
                    int databaseId = database.DatabaseID;
                    //Get Issuetype object

                    issue = new Issue();

                    //Get Issuetype object  And Create a new  Global issue object 
                    IssueType issueType = dc.IssueTypes.FirstOrDefault(i => i.IssueTypeId == IssueTypeId);

                    issue.IssueType = issueType;
                    issue.TargetId = database.DatabaseID;
                    issue.Description = issueType.Description.Replace("{Database}", database.name);

                    dc.Issues.InsertOnSubmit(issue);
                }

                dc.SubmitChanges();

            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }



    }
}
